# -*- coding: utf-8 -*-
# @Author   : zxc19
# @Time     : 2023/7/6 19:21
# @FileName : run.py
# @Desc     : 请输入功能模块描述
import os
from pathlib import Path

import pytest

from config import settings
from utils.bif_datetime import get_current_time
from utils.compressor import Compressor
from utils.send_email import EmailSender

if __name__ == '__main__':
    # 删除上一次运行的截图文件
    os.system(f"del {str(settings.SCREENSHOT_DIR.joinpath('*.png'))}")
    # 执行测试（前置需要配置pytest.ini）
    pytest.main(
        ["-vqs", f'--alluredir={str(settings.RESULT_DIR)}', "--clean-alluredir"])
    # 生成报告
    os.system(f'allure generate {str(settings.RESULT_DIR)} -o {str(settings.REPORT_DIR)} --clean')
    # 压缩报告和结果
    if settings.REPORT_ZIP_START:
        if not settings.REPORT_ZIP_DIR.exists():
            Path.mkdir(settings.REPORT_ZIP_DIR)
        zip_name = settings.REPORT_ZIP_DIR.joinpath(
            f'{settings.PROJECT_NAME}-{get_current_time("%Y%m%d%H%M%S")}.zip')
        zip_res = Compressor.compress(zip_name, [settings.RESULT_DIR, settings.REPORT_DIR])
        if zip_res and settings.EMAIL_START:
            # 压缩成功，则发送邮件
            sender = EmailSender()
            sender.send_email(eval(settings.EMAIL_RECV_USER), f'{settings.PROJECT_NAME}-WEB UI测试',
                              f'测试地址：{settings.BASE_URL}', attachment=zip_name)

